<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Wap\Controller;

use Think\Controller;
use Com\DingdingAuth;
/**
 * 后台用户控制器
 * 
 * @author jry <598821125@qq.com>
 */
class CustomerController extends WapController
{


    /**
     * 首页
     */
    public function index()
    {
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	    	 
    	 //搜索
			
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['customer_num|customer_name|mobile'] = array($condition, $condition, $condition,'_multi'=>true);
       $keywords=I('keywords');
	  
	  if(!empty($keywords)){
	  	$map['level']=$keywords;
	  }
        //获取所有用户
       $map['status'] = array('egt', '0'); //禁用和正常状态
        $data_list = D('Customer')->page(!empty($_GET["p"])?$_GET["p"]:1, 10)->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('Customer')->where($map)->count(), 10);

			foreach($data_list as $key=>&$val){
				$val['customer_service_id_text']=get_employee_info($val['customer_service_id'],'name');
				$tt['tablename']='customer_level';
				$tt['fieldvalue']=$val['level'];
				$customer_level=D('Descriptions')->where($tt)->find();
				$val['level']=$customer_level['title'];
				$rr['tablename']='customer_source';
				$rr['fieldvalue']=$val['source'];
				$customer_source=D('Descriptions')->where($rr)->find();
				$val['source']=$customer_source['title'];
				$val['city_text']=get_city_name($val['province']).'-'.get_city_name($val['city']);
				$tm['id']=$val['city'];
				$val['city']=D('Region')->where($tm)->getField('name' );
				//$val['customer_name']='<a href="'.U('Customer/xq',array('id'=>$val['id'])).'"style="text-decoration:none;">'.$val['customer_name'].'</a>';
		    }
		
			
		$customer_level=D('Descriptions')->where("tablename='customer_level'")->select();
	
				
    	$this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
    	$this->assign('page', $page->show());
    	$this->assign('data_list', $data_list);
    	$this->assign('customer_level', $customer_level);
    	
    	$this->assign('meta_title', '客户列表');
    	$this->display();
    
    }
    
    /**
     * 我的
     * 创建者或客服
     */
    public function my()
    {
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    
    	//搜索
    	$keyword = I('keyword', '', 'string');
    	$condition = array('like','%'.$keyword.'%');
    	$map['customer_num|customer_name|mobile'] = array($condition, $condition, $condition,'_multi'=>true);
    	$keywords=I('keywords');
    
    	if(!empty($keywords)){
    		$map['level']=$keywords;
    	}
    	//创建者或客服
    	$ding_id=$user_info['userid'];
    	$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('uid');
    	$map['create_member']=$uid;
    	$map['customer_service_id']=$uid;
    	 
    	$map['status'] = array('egt', '0'); //禁用和正常状态
    	$data_list = D('Customer')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
    	$page = new \Common\Util\Page(D('Customer')->where($map)->count(), C('ADMIN_PAGE_ROWS' ));
    
    	foreach($data_list as $key=>&$val){
    		$val['customer_service_id_text']=get_employee_info($val['customer_service_id'],'name');
    		$tt['tablename']='customer_level';
    		$tt['fieldvalue']=$val['level'];
    		$customer_level=D('Descriptions')->where($tt)->find();
    		$val['level']=$customer_level['title'];
    		$rr['tablename']='customer_source';
    		$rr['fieldvalue']=$val['source'];
    		$customer_source=D('Descriptions')->where($rr)->find();
    		$val['source']=$customer_source['title'];
    		$val['city_text']=get_city_name($val['province']).'-'.get_city_name($val['city']);
    		$tm['id']=$val['city'];
    		$val['city']=D('Region')->where($tm)->getField('name' );
    		//$val['customer_name']='<a href="'.U('Customer/xq',array('id'=>$val['id'])).'"style="text-decoration:none;">'.$val['customer_name'].'</a>';
    	}
    
    
    	$customer_level=D('Descriptions')->where("tablename='customer_level'")->select();
    
    	$this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
    	$this->assign('page', $page->show());
    	$this->assign('data_list', $data_list);
    	$this->assign('customer_level', $customer_level);
    	$this->assign('user_info', $user_info);
    	$this->assign('meta_title', '我的客户');
    	$this->display();
    
    }
    
    /**
     * 新增客户
     * */
    public  function  add(){
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	    	
    	if(IS_POST){
    		    		
    		//dump($_POST);exit;
    		$customer_object = D('Customer');
    		$data = $customer_object->create();
    		
    		$ding_id=$user_info['userid'];
    		$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('uid');
    		$data['create_member']=$uid;
    		$data['create_time']=time();
    		$data['last_service_time']=time();
    		if($data){
    			$id = $customer_object->add();
    			if($id){
    				$this->success('新增成功', U('index'));
    			}else{
    				$this->error('新增失败');
    			}
    		}else{
    			$this->error($customer_object->getError());
    		}
    	}else{
    		
    		$customer_level=D('Descriptions')->where('tablename="customer_level"')->field('fieldvalue as id,title')->select();
    		$customer_source=D('Descriptions')->where('tablename="customer_source"')->field('fieldvalue as id,title')->select();
    		$province=M("region")->where("pid=1")->select();
    		$kefu_arr=M("employee")->where("department=7")->field("uid as id,name as title")->select();
  		
    		
    		$this->assign('province', $province);
    		$this->assign('kefu_arr', $kefu_arr);
    		$this->assign('customer_level', $customer_level);
    		$this->assign('customer_source', $customer_source);
    		$this->assign('USER_SEX_LIST', C('USER_SEX_LIST'));
    		
    		$this->assign('meta_title', '新增客户');
    		$this->display();
    	}
    }
    /**
     *编辑客户
     */
    public  function edit($id){		
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	
    	$info = D('Customer')->find($id);
	    
	    if(IS_POST){
	    	$customer_object = D('Customer');
            $customer_object->create();
            if($customer_object->save()){
            	
                $this->success('更新成功', U('index'));
            }else{
                
                $this->error('更新失败', $customer_object->getError());
            }
	    }else{
	    	
	    	$customer_level=D('Descriptions')->where('tablename="customer_level"')->field('fieldvalue as id,title')->select();
    		$customer_source=D('Descriptions')->where('tablename="customer_source"')->field('fieldvalue as id,title')->select();
    		$province=M("region")->where("pid=1")->select();
    		$kefu_arr=M("employee")->where("department=7")->field("uid as id,name as title")->select();
    		$city=M('region')->where(array('pid'=>$info['province']))->select();

	    	$this->assign('info', $info);
		    $this->assign('province', $province);$this->assign('city', $city);
    		$this->assign('kefu_arr', $kefu_arr);
    		$this->assign('customer_level', $customer_level);
    		$this->assign('customer_source', $customer_source);
    		$this->assign('USER_SEX_LIST', C('USER_SEX_LIST'));
    		
    		$this->assign('meta_title', '编辑客户');
    		$this->display();
	    }
    }
    
    /**
     * 根据省份获取城市
     */
    public function getCityAjax(){
    
    	$keyword = I('keyword');
    	
    	$map['pid']=$keyword;
    	$list=M('region')->where($map)->select();
    	foreach ($list as $k => $vo) {
    
    		$data .= "<option value ='" . $vo['id'] . "'>" . $vo['name']."</option>";
    			
    	}
    	$this->ajaxReturn($data);
    }
    /**
     * 删除客户
     */
//     public function delCustomerAjax(){
    
//     	$id = I('id');
//     	$re=M('Customer')->where('id='.$id)->delete();
//     	if ($re) {
//     		$data['info']=1;
//     	}else{
//     		$data['info']=0;
//     	}
//     	$this->ajaxReturn($data);
//     }
    /**
     * 客户详情页
     */
    public function customerinfo(){
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	$ding_id=$user_info['userid'];
    	$ding_pass=M('Employee')->where(array('ding_id'=>$ding_id))->getField('ding_pass');
    	$user_info['ding_pass']=$ding_pass;
    	$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('uid');
    	$id=I('id');
    	$map['id']=$id;
		$val = M('Customer')->where($map)->find();
		
		if ($uid==$val['create_member'] || $uid==$val['customer_service_id']) {
			$val['creater']=1;
		}
		$val['create_member'];
		
		$val['customer_service_id_text']=get_employee_info($val['customer_service_id'],'name');
		$tt['tablename']='customer_level';
		$tt['fieldvalue']=$val['level'];
		$customer_level=D('Descriptions')->where($tt)->find();
		$val['level']=$customer_level['title'];
		$rr['tablename']='customer_source';
		$rr['fieldvalue']=$val['source'];
		$customer_source=D('Descriptions')->where($rr)->find();
		$val['source']=$customer_source['title'];
		$val['city_text']=get_city_name($val['province']).get_city_name($val['city']).$val['address'];
		$tm['id']=$val['city'];
		$val['city']=D('Region')->where($tm)->getField('name');
		$val['create_member']=get_employee_info($val['create_member'],'name');
		$val['create_time']=date('Y/m/d',$val['create_time']);
		
		switch($val['sex']){
			case '1':
			$val['sex']="男";
			break;
			case '-1':
			$val['sex']="女";
			break;
			case '0':
			$val['sex']="保密";
			break;
		}

		$this->assign('user_info', $user_info);
    	$this->assign('val', $val);
    	$this->assign('meta_title', '客户详情');
    	$this->display();
    }

    
}   
